<?php

namespace app\admin\model\card;

use think\Model;


class Cardlog extends Model
{

    

    

    // 表名
    protected $name = 'view_card_log';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = false;

    // 定义时间戳字段名
    protected $createTime = false;
    protected $updateTime = false;
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'addtime_text'
    ];

    public $user_id = '';
    public $card_id = '';
    public $log_id = '';
    public $day_nums = '';
    public $addtime = '';


    public function getAddtimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['addtime']) ? $data['addtime'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }

    protected function setAddtimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }


    public function user()
    {
        return $this->belongsTo('app\admin\model\User', 'user_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }


    public function card()
    {
        return $this->belongsTo('app\admin\model\view\Card', 'card_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }


    public function carduser()
    {
        return $this->belongsTo('app\admin\model\card\Carduser', 'log_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }

    public function add(){
        $data = [
          'user_id' => $this->user_id,
          'card_id' => $this->card_id,
          'log_id' => $this->log_id,
          'day_nums' => $this->day_nums,
          'addtime' => $this->addtime,
        ];

        $this->insert($data);
    }
}
